package yadong.example.code.jaxb;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import org.springframework.web.servlet.view.AbstractView;


public class GoGProfileXMLView extends AbstractView {
    private static final String DEFAULT_CONTENT_TYPE = "text/xml";
    
    public GoGProfileXMLView() {
        super();
        setContentType(DEFAULT_CONTENT_TYPE);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType(getContentType());
        response.setHeader("Content-Disposition", "inline;filename=gog-profile.xml");
        writeGoGEidosXML(model, request, response);
    }
    
    @SuppressWarnings("unchecked")
    protected void writeGoGEidosXML(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Doc data =(Doc)model.get("doc");
        if(data!=null){
	        JAXBContext jaxbContext = JAXBContext.newInstance(Doc.class);
	        Marshaller marshaller = jaxbContext.createMarshaller();
	        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT , new Boolean(true));
	        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
	        if(data!=null){
	        	marshaller.marshal(data,response.getWriter()); 
	        }
        }
    }
   
}
